#ifndef IMG_WIDGET_H
#define IMG_WIDGET_H

#include <QObject>

#include "img_file.h"
#include "vtk_crossline.h"

#include <vtkSmartPointer.h>
#include <vtkResliceImageViewer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkImagePlaneWidget.h>

#include <vtkCommand.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyle.h>
#include <vtkCoordinate.h>
#include <vtkCamera.h>
#include <vtkProperty.h>

#include <vtkResliceCursorWidget.h>
#include <vtkImageMapToWindowLevelColors.h>

#include <vtkLookupTable.h>
#include <vtkImageActor.h>
#include <vtkImageProperty.h>

class vtkCallback;
class vtkCrossLineStyle;

class img_Widget: public QObject
{

    Q_OBJECT

private:
    vtkRenderWindow * const m_window;
    vtkSmartPointer<vtkRenderer> m_render;
    vtkSmartPointer<vtkImagePlaneWidget> m_ipw;
    vtkSmartPointer<vtkResliceImageViewer> m_viewer;
    vtkSmartPointer<vtkResliceImageViewer> m_analysis;

    vtkSmartPointer<vtkCrossLineStyle> m_crossLineStyle;
    vtkSmartPointer<vtkInteractorObserver> m_defaultStyle;

    vtkSmartPointer<vtkLookupTable> m_colorTable;

    vtk_CrossLine * m_crossLine{nullptr};
    img_File * m_img{nullptr};

    bool anlsExist{false};

    quint8 m_type;

public:
    const static quint8 AXIAL = 2;
    const static quint8 CORONAL = 1;
    const static quint8 SAGITTAL = 0;

public:
    img_Widget(vtkRenderWindow *_renderwindow, quint8 _type, QObject *parent = nullptr);
    ~img_Widget();
    void setImg(img_File * img);
    void setAnalysisImg(vtkImageData *img);
    void unSetAnalysisImg();
    void setCenter(double _center[3], const quint8 type);
    void setWindowLevel(double _window, double _level);
    void setCrossLineMode(bool mode);
    void setLabelOpacity(int _value);

public: //getter
    bool isOpen();
    bool isAnlsExist();
    quint8 getType();
    const img_Info * getImgInfo();
    vtk_CrossLine * getCrossLine();
    void getWindowLevel(double wl[2]);
    vtkCrossLineStyle * getCrossLineStyle();
    vtkInteractorObserver * getDefaultStyle();
    vtkRenderWindow * getRenderWindow();
    vtkResliceImageViewer * getAnalysisViewer();
    vtkResliceImageViewer * getImageViewer();
    vtkImagePlaneWidget * getImagePlaneWidget();

signals:
    void sliceChange(int _slice);
};

class vtkCrossLineStyle: public QObject, public vtkInteractorStyle
{
    Q_OBJECT
public:
    img_Widget * widget{nullptr};
    bool isLeftButtonDown{false};

    static vtkCrossLineStyle* New()
    {
        return new vtkCrossLineStyle();
    }

    virtual void OnLeftButtonDown()override
    {
        isLeftButtonDown = true;
        update();
    }

    virtual void OnMouseMove()override
    {
        if(isLeftButtonDown) {
            update();
        }
    }

    virtual void OnLeftButtonUp()override
    {
        isLeftButtonDown = false;
    }

private:
    void update()
    {
        vtkSmartPointer<vtkCoordinate> pCoorPress = vtkSmartPointer<vtkCoordinate>::New();
        pCoorPress->SetCoordinateSystemToDisplay();
        pCoorPress->SetValue(
            GetInteractor()->GetEventPosition()[0],
            GetInteractor()->GetEventPosition()[1]
        );
        double *worldCoord;
        if(widget->isAnlsExist()) {
            worldCoord = pCoorPress->GetComputedWorldValue(
                             widget->getAnalysisViewer()->GetRenderer());
        } else {
            worldCoord = pCoorPress->GetComputedWorldValue(
                             widget->getImageViewer()->GetRenderer());
        }
        widget->getCrossLine()->setCenter(worldCoord);
        widget->getImageViewer()->GetRenderWindow()->Render();
        emit centerChange();
    }

signals:
    void centerChange();
};

#endif // IMG_WIDGET_H
